package com.siashan.unipay4j.ali.pay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.siashan.unipay4j.core.IPayConfig;
import com.siashan.unipay4j.core.Result;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 手机网站支付
 *
 *
 * 手机网站支付是指商家在移动端网页展示商品或服务，
 * 用户在商家页面确认使用支付宝支付后，浏览器自动跳转支付宝 App 或支付宝网页完成付款的支付产品。该产品在签约完成后，需要技术集成方可使用。
 *
 *
 * see https://opendocs.alipay.com/open/203/105288?pathHash=f2308e24
 *
 * @author  siashan
 * @since  v1.0.1
 **/
@Slf4j
public class AliPayWap extends AliPay {


    /**
     * 销售产品码，商家和支付宝签约的产品码
     */
    private static final String PRODUCT_CODE = "QUICK_WAP_WAY";

    public AliPayWap(IPayConfig aliPaySet) {
        super(aliPaySet);
    }

    public AliPayWap(){
        super();
    }


    /**
     * 支付
     *
     * @param  params  支付请求参数
     * @return 支付返回结果
     * @throws AlipayApiException
     */
    @Override
    public Result executePay(Map<String, Object> params) throws AlipayApiException {
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        alipayRequest.setReturnUrl(payConfig.getReturnUrl());
        alipayRequest.setNotifyUrl(payConfig.getNotifyUrl());
        params.put("product_code", PRODUCT_CODE);
        alipayRequest.setBizContent(JSON.toJSONString(params));
        AlipayTradeWapPayResponse r = this.alipayClient.pageExecute(alipayRequest);
        if (r.isSuccess()) {
            return Result.tradeSuccess(r.getBody());
        } else {
            return Result.tradeFail( r.getSubCode(), r.getSubMsg());
        }
    }

    /**
     * 交易类型
     *
     * @return 交易类型
     */
    @Override
    public AliTradeTypeEnum getTradeType() {
        return AliTradeTypeEnum.WAP;
    }


    @Override
    public boolean needBuyerId() {
        return false;
    }
}
